/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.sys.service.SystemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.UserBusiness;
import com.thinkgem.jeesite.modules.sys.service.UserBusinessService;

import java.util.ArrayList;
import java.util.List;

/**
 * 商家用户信息Controller
 * @author 开发
 * @version 2016-06-19
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/userBusiness")
public class UserBusinessController extends BaseController {

	@Autowired
	private UserBusinessService userBusinessService;

	@Autowired
	private SystemService systemService;
	
	@ModelAttribute
	public UserBusiness get(@RequestParam(required=false) String id) {
		UserBusiness entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = userBusinessService.get(id);
		}
		if (entity == null){
			entity = new UserBusiness();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:userBusiness:view")
	@RequestMapping(value = {"list", ""})
	public String list(UserBusiness userBusiness, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<UserBusiness> list = new ArrayList<UserBusiness>();
		Page<UserBusiness> page = userBusinessService.findPage(new Page<UserBusiness>(request, response), userBusiness);
		for(UserBusiness business : page.getList()){
			business.setUser(systemService.getUser(business.getId()));
			list.add(business);
		}
		page.setList(list);
		model.addAttribute("page", page);
		return "modules/sys/userBusinessList";
	}

	@RequiresPermissions("sys:userBusiness:view")
	@RequestMapping(value = "form")
	public String form(UserBusiness userBusiness, Model model) {
		model.addAttribute("userBusiness", userBusiness);
		return "modules/sys/userBusinessForm";
	}

	@RequiresPermissions("sys:userBusiness:edit")
	@RequestMapping(value = "save")
	public String save(UserBusiness userBusiness, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, userBusiness)){
			return form(userBusiness, model);
		}
		userBusinessService.save(userBusiness);
		addMessage(redirectAttributes, "保存信息成功");
		return "redirect:"+Global.getAdminPath()+"/sys/userBusiness/?repage";
	}
	
	@RequiresPermissions("sys:userBusiness:edit")
	@RequestMapping(value = "delete")
	public String delete(UserBusiness userBusiness, RedirectAttributes redirectAttributes) {
		userBusinessService.delete(userBusiness);
		addMessage(redirectAttributes, "删除信息成功");
		return "redirect:"+Global.getAdminPath()+"/sys/userBusiness/?repage";
	}

}